package com.smartnews.jpa_entity_generator.rule;

import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Rule used to generate imports.
 */
@Data
public class ImportRule implements Serializable, ClassMatcher {

    private String className;
    private List<String> classNames = new ArrayList<>();
    private String importValue;

    public boolean importValueContains(String className) {
        if (importValue.startsWith("static")) {
            return false;
        }
        if (importValue.endsWith(".*")) {
            return className.replaceFirst("\\.[^\\.]+$", ".*").equals(importValue);
        } else {
            return className.equals(importValue);
        }
    }

    public static ImportRule createGlobal(String importValue) {
        ImportRule rule = new ImportRule();
        rule.setImportValue(importValue);
        return rule;
    }

    // add by adrninistrator

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ImportRule that = (ImportRule) o;
        return importValue.equals(that.importValue);
    }

    @Override
    public int hashCode() {
        return Objects.hash(importValue);
    }
}